package tech.aistar.corejava.day03;

/**
* @Description:    作用描述:定义一个变量,求出它对应的二进制的数
* @Author:          lya
* @CreateDate:     2019/7/17 10:25
* @Version:        1.0
*/
public class BiraryDemo {
    public static void main(String[] args) {
        int var = 8;
        String str = "";
        //定义一个变量记录精简二进制位数
        int count = 0;
        int m = -1;
        while (true){
            //求余数
            int y = var % 2;
            //求商
            int s = var/2;
            //计数
            count++;
            //拼接字符串
            str = y + str;
            //循环退出的条件
            if (s == 0){
                break;
            }
            var = s;
        }
        System.out.println(str);
        System.out.println("位数:"+count);
        String zero = "";
        for(int i = 0;i <= 32 - count;i++){
            zero +="0";
            m++;
            if(m == 8){
                zero += " ";
                m = 0;
            }
        }
        System.out.print(zero + str);
    }
}
